package com.cloudsigns.factorymanager2.produce.model;

import android.util.Log;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartScheduleBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

public class ProductUnusualFeedbackConfirmModel implements IProductUnusualFeedbackConfirmModel {

    @Override
    public void productUnusualFeedbackConfirm(final String orderId, final String productId, final CommonEnum.WorkPartType type, final String workPartFeedbackId, final String workPartId, final String workPartOrderId, final List<WorkPartScheduleBean> workPartScheduleItemRequestList, final OnProductUnusualFeedbackConfirmListener onProductUnusualFeedbackConfirmListener) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    HttpClient client = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(ProjectUtil.PRODUCT_UNUSUAL_FEEDBACK_CONFIRM);
                    HttpResponse response;
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);

                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("orderId", orderId);
                    jsonObject.put("productId", productId);
                    jsonObject.put("type", type);
                    jsonObject.put("workPartFeedbackId", workPartFeedbackId);
                    jsonObject.put("workPartId", workPartId);
                    jsonObject.put("workPartOrderId", workPartOrderId);

                    if (null != workPartScheduleItemRequestList && workPartScheduleItemRequestList.size() > 0) {
                        JSONArray workPartScheduleJsonArr = new JSONArray();

                        for (int i = 0; i < workPartScheduleItemRequestList.size(); i++) {
                            WorkPartScheduleBean workPartScheduleBean = workPartScheduleItemRequestList.get(i);
                            JSONObject workPartScheduleJsonObj = new JSONObject();
                            workPartScheduleJsonObj.put("amount", workPartScheduleBean.getAmount());
                            workPartScheduleJsonObj.put("scheduleDate", workPartScheduleBean.getScheduleDate());

                            workPartScheduleJsonArr.put(workPartScheduleJsonObj);
                        }
                        jsonObject.put("workPartScheduleItemRequestList", workPartScheduleJsonArr);
                    } else {
                        jsonObject.put("workPartScheduleItemRequestList", new JSONArray());
                    }

                    Log.e("ProductUnusualFeedbackC", "jsonObject=" + jsonObject.toString());
                    Log.e("ProductUnusualFeedbackC", "ProjectUtil=" + ProjectUtil.PRODUCT_UNUSUAL_FEEDBACK_CONFIRM);

                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);
                    httpPost.setEntity(se);
                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    Log.e("ProductUnusualFeedbackC", "result=" + result);

                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != onProductUnusualFeedbackConfirmListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            onProductUnusualFeedbackConfirmListener.successed();
                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }
                            onProductUnusualFeedbackConfirmListener.failed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnProductUnusualFeedbackConfirmListener {

        void successed();

        void failed(String desc);
    }
}
